<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
class Job_model extends MY_Model
{
    private $table = 'job';
	public function __construct()
	{
		parent::__construct($this->table);	
	}
	
	
	/**
	 * 获取职位信息
	 * */
	public function getList($select='*',$offset=0,$limit=20,$search='',$where=array(),$order=''){
        if($search){        //分类ID
            $this->db->like('type.typeName',$search);            
        }
        if($where){        //分类ID
            $this->db->where($where);
        }
        $this->db->select($select);
        $this->db->where('job.status',0);   //只获取正常状态的职位,1态为关闭态
        $this->db->where('company.issue',1); //发布
        $this->db->where('company.delFlag',0);//未删除状态  
        
        $this->db->join('type','type.typeId=job.typeId','left');        //职位ID
        $this->db->join('type as exp','exp.typeId=job.expId','left');        //经验分类ID
        $this->db->join('type as salary','salary.typeId=job.salaryId','left');        //薪资分类ID
        
       
        $this->db->join('company','company.companyId=job.companyId');        //职位
        $this->db->join('city','city.cityId=company.cityId','left');        //城市ID
        $this->db->group_by('job.jobId');
        if($order){
            //推荐排序规则      
        }
        $this->db->order_by('job.createTime','desc');
        $res=$this->db->get($this->table,$limit,$offset)->result_array();
        return $res;
    }
    
    /**
     * 公司端获取职位列表
     * */
    public function getListByCompanyId($companyId,$select='*'){
        
        $this->db->select($select);   
        $this->db->where('job.companyId',$companyId);
        
        $this->db->join('type','type.typeId=job.typeId','left');        //职位ID
        $this->db->join('type as exp','exp.typeId=job.expId','left');        //经验分类ID
        $this->db->join('type as salary','salary.typeId=job.salaryId','left');        //薪资分类ID
        $this->db->join('deliver','deliver.jobId=job.jobId','left');
        $this->db->group_by('job.jobId');        
        $this->db->order_by('job.createTime','desc');
        $res=$this->db->get($this->table)->result_array();
        return $res;
    }
        
    //获取职位详情
    public function getDetail($jobId,$select="*"){  
        $this->db->select($select);
        $this->db->where('jobId',$jobId);
        $this->db->join('company','company.companyId=job.companyId');
        $this->db->join('type','type.typeId=job.typeId');
        $this->db->join('type as exp','exp.typeId=job.expId','left');
        $this->db->join('type as salary','salary.typeId=job.salaryId','left');
        $this->db->join('type as scale','scale.typeId=company.scaleId','left');
        $this->db->join('type as trade','trade.typeId=company.tradeId','left');
        $this->db->group_by('job.jobId');
        $res=$this->db->get($this->table)->row_array();
        return $res;
    }
    //更新点赞,评论,收藏,浏览量等数据
    public function updateNum($jobId,$data){
        foreach ($data as $k => $v)
        {
            $this->db->set($k, $v, FALSE);
        }
        $this->db->where('jobId', $jobId)->update($this->table);
        return $this->db->affected_rows();
    }
    //获取数据条数
    public function getCount($search){
        if($search){
            $this->db->like('title',$search);
        }
        return $this->db->get($this->table)->num_rows();
    }
    //后台获取职位数据
    public function getList_admin($page=0,$limit=20,$search=''){
        //$this->db->where();
        if($search){
            $this->db->like('title',$search);
        }
        
        $res=$this->db->order_by('createTime','desc')->get($this->table,$limit,$page*$limit)->result_array();
        return $res;
    }
	//新增数据
	public function add($data){
	    $data['createTime']=time();
	    $this->db->insert($this->table,$data);
	    return $this->db->insert_id();
	}
	//更新数据
	public function update($id,$data){
	    $data['updateTime']=time();
	    $this->db->where('jobId',$id)->update($this->table,$data);
	    return $this->db->affected_rows();
	}
	//删除单个或多个数据
	public function del($jobIds){
	    $this->db->where_in('jobId',$jobIds);
	    $this->db->delete($this->table);
	    return $this->db->affected_rows();
	    
	}
}
?>